// 封装的一个JQuery小插件
jQuery.fn.rowspan = function (colIdx) {
    return this.each(function () {
        var that;
        $('tr', this).each(function (row) {
            $('td:eq(' + colIdx + ')', this).filter(':visible').each(function (col) {
                if (that != null && $(this).html() == $(that).html()) {
                    rowspan = $(that).attr("rowSpan");
                    if (rowspan == undefined) {
                        $(that).attr("rowSpan", 1);
                        rowspan = $(that).attr("rowSpan");
                    }
                    rowspan = Number(rowspan) + 1;
                    $(that).attr("rowSpan", rowspan);
                    $(this).hide();
                } else {
                    that = this;
                }
            });
        });
    });
};

var yeeyoo = (function ($, window, document) {
    function yeeyoo() {
        var This = this;
        $(function () {
            This.init();
        })
    }
    yeeyoo.prototype = {
        isIe: function (ie) {
            var userAgent = navigator.userAgent;
            var isOpera = userAgent.indexOf("Opera") > -1;
            var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera;
            var isFF = userAgent.indexOf("Firefox") > -1;
            var isSafari = userAgent.indexOf("Safari") > -1;
            if (isIE) {
                var IE7 = IE8 = false;
                var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
                reIE.test(userAgent);
                var fIEVersion = parseFloat(RegExp["$1"]);
                IE7 = fIEVersion == 7.0;
                IE8 = fIEVersion == 8.0;
                IE9 = fIEVersion == 9.0;
                if (ie) { return "IE" }
                if (IE7) { return "IE7"; }
                if (IE8) { return "IE8"; }
                if (IE9) { return "IE9"; }
            } //isIE end
            if (isFF) {
                return "FF";
            }
        },
        returnTop: function (count) { /*返回顶部*/
            if (typeof count != "number") {
                count = 0
            }
            $('html,body').stop().animate({ scrollTop: count }, 300)
        },
        pageMinHeight: function () {
            if ($(".ui-cont").length > 0) {
                $(".ui-cont").css({ "min-height": $(window).height() - $(".ui-cont").offset().top - $(".footer").height() - $(".current-page").height() })
            }
        },
        msgCode: function (paramete) {
            var obj = paramete["obj"]; //按钮对象
            var time = paramete["lasttime"]; //读秒剩余时间
            var miao = time;
            var timer = null,
				txt = null;
            var bggreen = paramete["c1"] || "";
            var bggray = paramete["c2"] || "disabled";
            var btnhui = $(obj).removeClass(bggreen).addClass(bggray);
            var txt = paramete["customTxt"] || "秒后可重新获取";
            var defaultTxt = paramete["defaultTxt"] || "获取短信验证码";
            var reset = function () {
                clearInterval(timer);
                $(obj).prop("disabled", false);
                $(obj).removeClass(bggray).addClass(bggreen).val(defaultTxt);
            }
            //初始化发送按钮，清空初始化事件
            btnhui.val(miao + txt);
            $(obj).prop("disabled", true);
            //发送倒数读秒
            timer = setInterval(function () {
                miao--;
                btnhui.val(miao + txt);
                if (miao == 0) {
                    reset();
                    if (paramete["endfn"] != null && typeof paramete["endfn"] == "function") paramete["endfn"]();
                }
            }, 1000);
            return { reset: reset }
        },
        inputTip: function () {
            if (this.isIe() != "IE7" && this.isIe() != "IE8" && this.isIe() != "IE9") {
                return false
            }
            $("input[placeholder]").each(function () {
                if ($(this).attr("type") != "password") {
                    if ($(this).val() == $(this).attr("placeholder")) {
                        $(this).val("")
                    }
                    if ($(this).val() == "") {
                        $(this).val($(this).attr("placeholder")).css("color", "#ccc");
                    } else {
                        $(this).css("color", "#333");
                    }

                    $(this).on("focus", function () {
                        if ($(this).val() == $(this).attr("placeholder")) {
                            $(this).val("").css("color", "#333");
                        }
                    })
                    $(this).on("blur", function () {
                        if ($(this).val() == "" || $(this).val() == $(this).attr("placeholder")) {
                            $(this).val($(this).attr("placeholder")).css("color", "#ccc");
                        }
                    })
                } else {
                    $(this).hide();
                    $(this).after("<input type='text'>");
                    $(this).next().attr({
                        "class": $(this).attr("class"),
                        "name": $(this).attr("name") || "",
                        "value": $(this).attr("placeholder")
                    }).css("color", "#ccc").removeClass("eyeThis");
                    var txt1 = $(this).attr("placeholder");
                    $(this).next().on("focus", function () {
                        if ($(this).val() == txt1) {
                            $(this).val("").css("color", "#333");
                            $(this).prev().val("");
                        }
                        if ($(this).parent().find(".showPwd").length > 0 && $(this).parent().find(".showPwd").hasClass("open")) {
                            $(this).prev().hide();
                            $(this).show().focus();
                        } else {
                            $(this).prev().show().focus();
                            $(this).hide();
                        }
                    })
                    $(this).on("blur", function () {
                        if ($(this).val() == "") {
                            $(this).next().val(txt1).css("color", "#ccc");
                            $(this).next().show();
                            $(this).hide();
                        }
                    });
                    $(this).next().on("blur", function () {
                        if ($(this).val() == "" || $(this).val() == txt1) {
                            $(this).val(txt1).css("color", "#ccc");
                        }
                    })
                }
            })
        },
        beautifyCheck: function () {
            if (this.isIe() == "IE7" || this.isIe() == "IE8") { return false }
            $("[type='checkbox']").each(function () {
                if ($(this).hasClass("noCreat") || $(this).parents("label").attr("on") == "false" || $(this).parents("label").attr("on") == "true") { return }
                $(this).parents("label").attr("on", this.checked.toString());
                $(this).after("<span class='checkboxSpan' style='width:" + $(this).width() + "px;height:" + $(this).width() + "px; vertical-align:" + $(this).css("vertical-align") + ";'></span>");
                $(this).parents("label").on("click", function () {
                    $("[type='checkbox']", this).attr("checked", $("[type='checkbox']", this)[0].checked);
                    $(this).attr("on", $("[type='checkbox']", this)[0].checked.toString())
                })
                $(this).css({ position: "fixed", left: "-10000px", opacity: .01 })
            });
        },
        beautifyRadio: function () {
            var ele = ".carrys[type='radio']"
            if (this.isIe() == "IE7" || this.isIe() == "IE8") { return false }
            $(ele).each(function () {
                if ($(this).hasClass("noCreat") || $(this).parents("label").attr("on") == "false" || $(this).parents("label").attr("on") == "true") { return }
                $(this).parents("label").attr("on", this.checked.toString());
                $(this).after("<span class='radioEle' style='width:" + $(this).width() + "px;height:" + $(this).width() + "px; vertical-align:" + $(this).css("vertical-align") + ";'><em></em></span>");
                $(this).parents("label").on("click", function () {
                    if ($(this).attr("on") == "true") { return false; }
                    $(ele).each(function (index) {
                        $(this).parents("label").attr("on", this.checked.toString());
                        console.log(this.checked);
                    })
                })
                $(this).css({ position: "fixed", left: "-10000px", opacity: .01 })
            });
        },
        slideNav: function () {
            $(".slide-nav li").each(function () {
                var This = this;
                $(this).on("click", function () {
                    if ($(".subName", this).hasClass("block")) {
                        $(".subName", this).slideUp(250, function () {
                            $(this).removeClass("block");
                            $(this).parents("li").removeClass("open");
                        });
                    } else {
                        $(".subName", this).slideDown(250, function () {
                            $(this).addClass("block");
                            $(this).parents("li").addClass("open");
                        });

                    }
                })
            })
        },
        lookPwd: function () {
            var That = this;
            if ($(".lookPwd").length == 0) { return false; }
            $(".lookPwd").each(function () {
                var This = this;
                $(".showPwd", this).on("click", function () {
                    if (!$(this).hasClass("open")) {
                        $(this).addClass("open");
                        if (That.isIe() != "IE7" && That.isIe() != "IE8" || $(".eyeThis", This).val() == "") {
                            $(".eyeThis", This).prop("type", "text");
                        } else {
                            $(".eyeThis", This).hide();
                            $(".eyeThis", This).next().show().val($(".eyeThis", This).val());
                        }
                    } else {
                        $(this).removeClass("open");
                        if (That.isIe() != "IE7" && That.isIe() != "IE8" || $(".eyeThis", This).val() == "") {
                            $(".eyeThis", This).prop("type", "password");
                        } else {
                            $(".eyeThis", This).show().val($(".eyeThis", This).next().val());
                            $(".eyeThis", This).next().hide();
                        }
                    }
                })
            })
        },
        countDown: function (option) {
            /*倒计时*/
            var time_now_server, time_now_client, time_end, time_server_client, timerID;
            if (option["endTime"]) $(option["obj"]).attr("data-end", option["endTime"])
            if (option["startTime"]) $(option["obj"]).attr("data-start", option["startTime"])
            time_end = new Date($(option["obj"]).attr("data-end")); //结束的时间
            time_end = time_end.getTime();
            time_now_server = new Date($(option["obj"]).attr("data-start")); //开始的时间
            time_now_server = time_now_server.getTime();
            time_now_client = new Date();
            time_now_client = time_now_client.getTime();
            time_server_client = time_now_server - time_now_client;
            setTimeout(show_time, 100);
            function show_time() {
                var timer = $(option["obj"]);
                timer.each(function () {
                    var o = {
                        day: $(".day", this),
                        hour: $(".hour", this),
                        allhour: $(".allhour", this),
                        min: $(".min", this),
                        sec: $(".sec", this),
                        hm: $(".hm", this)
                    }
                    var This = this;
                    if (!timer) {
                        return;
                    }
                    var time_now, time_distance, strday, strhour, strmini, strsec, strhm;
                    var day, hour, allhour, minute, second, hm;
                    var time_now = new Date();
                    time_now = time_now.getTime() + time_server_client;
                    time_distance = time_end - time_now;
                    if (time_distance > 0) {
                        day = Math.floor(time_distance / 86400000)
                        //						time_distance -= day * 86400000;
                        hour = Math.floor(time_distance / 3600000) % 24
                        //						time_distance -= hour * 3600000;
                        allhour = Math.floor(time_distance / (86400000 / 24))
                        time_distance -= allhour * (86400000 / 24);
                        //						time_distance -= allhour * 3600;
                        minute = Math.floor(time_distance / 60000)
                        time_distance -= minute * 60000;
                        second = Math.floor(time_distance / 1000)
                        hm = Math.floor((time_distance % 1000) / 100)
                        if (day < 10)
                            day = "0" + day;
                        if (hour < 10)
                            hour = "0" + hour;
                        if (allhour < 10)
                            allhour = "0" + allhour;
                        if (minute < 10)
                            minute = "0" + minute;
                        if (second < 10)
                            second = "0" + second;
                        strday = day + "";
                        strhour = hour + "";
                        strallhour = allhour + "";
                        strmini = minute + "";
                        strsec = second + "";
                        strhm = hm + "";
                        timeSt(o.day, strday);
                        timeSt(o.hour, strhour);
                        timeSt(o.allhour, strallhour);
                        timeSt(o.min, strmini);
                        timeSt(o.sec, strsec)
                        timeSt(o.hm, strhm)
                        setTimeout(show_time, 100);
                    } else {
                        timeSt(o.day, "00");
                        timeSt(o.hour, "00");
                        timeSt(o.allhour, "00");
                        timeSt(o.min, "00");
                        timeSt(o.sec, "00")
                        timeSt(o.hm, "00")
                        if (option["callback"]) {
                            option["callback"]();
                        }
                        clearTimeout(timerID)
                    }
                })
                function timeSt(ob, t) {
                    if (t) {
                        for (var i = 0; i <= t.length; i++) {
                            ob.eq(i).html(ob.length != 1 ? t.charAt(i) : t)
                        }
                    }
                }
            }
        },
        init: function () {
            $(".returnTop").on("click", this.returnTop);//返回顶部
            this.pageMinHeight();//设置页面最小内容高度
            this.inputTip()//兼容低版本IE输入框提示
            this.beautifyCheck();//美化多选框
            this.beautifyRadio()//美化单选框
            this.slideNav();//左侧menu 菜单
            this.lookPwd()//显示输入密码

            $(".slide-nav").css({ height: $(".slide-nav").parent().outerHeight() })//
        }
    }
    return new yeeyoo()
})(jQuery, window, document)


/*
 @弹出层模板
  */

/* @默认
 * @ msg       => 文本信息               不可省略
 * @ btnVal    => 按钮名称               可省略
 * @ endfn     => 确定回调函数        可省略
 * */

function alerts(msg, btnVal, endfn) {
    var btn = btnVal || ["确&nbsp;&nbsp;定", "取&nbsp;&nbsp;消"];
    if (typeof btnVal == "function") {
        endfn = btnVal;
        btn = ["确&nbsp;&nbsp;定", "取&nbsp;&nbsp;消"];
    }
    new pop({
        type: "concise",
        content: msg,
        removeMask: false,
        btn: btn,
        callBack1: endfn || function () { }
    })
}
/*
 * @ 黑色提示
 * @ msg => 文本信息                  不可省略
 * @ s   => 倒计时秒数              可省略
 * @ endfn => 倒计时结束回调   可省略
 * */

function blackAlerts(msg, s, endfn) {
    var time = 2;
    if (typeof s == "function") {
        endfn = s
    } else {
        time = s || 2
    }
    new pop({
        type: "blackTips",
        content: msg,
        opacity: .0001,
        time: time,
        timeFn: endfn || function () { }
    })
}


/* @ 模板二
 * @ msg       => 文本信息               不可省略
 * @ btnVal    => 按钮名称               可省略
 * @ endfn     => 确定回调函数       可省略    只需按钮回调时 ：     subAlerts(msg,function(){//endfn},function(){//closeFn})
 * @ closeFn   => 确定回调函数    可省略    只需要关闭回调时 ： subAlerts(msg,false,function(){//closeFn})
 * */
function subAlerts(msg, btnVal, endfn, closeFn) {
    if (typeof btnVal != "string") {
        closeFn = endfn;
        endfn = btnVal;
        btnVal = "确&nbsp;&nbsp;定";
    }
    new pop({
        type: "singleBtn",
        boxStyle: "min-width:500px",
        content: msg,
        removeMask: false,
        btn: [btnVal],
        callBack1: endfn || function () { },
        callBack0: closeFn
    })
}

